package com.itheima.web;

import com.itheima.service.OrderSettingService;
import com.itheima.support.Result;
import com.itheima.utils.POIUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderseting")
public class OrderSettingController {

    @DubboReference
    private OrderSettingService orderSettingService;

    @PostMapping("/importData")
    @PreAuthorize("hasAuthority('import_orderseting')")
    public void importData(@RequestParam("file")MultipartFile multipartFile) throws IOException {
        List<String[]> list = POIUtils.readExcel(multipartFile.getInputStream(), multipartFile.getOriginalFilename());
        orderSettingService.importData(list);

    }

    @GetMapping("/findByYearMonth/{yearMonth}")
    @PreAuthorize("hasAuthority('search_orderseting')")
    public Result<Map>findByYearMonth(@PathVariable("yearMonth") String yearMonth ){

        return Result.build(orderSettingService.findByYearMonth(yearMonth));

    }

    @GetMapping("/updateOrderSetting/{orderDate}/{num}")
    @PreAuthorize("hasAuthority('uptate_orderseting')")
    public Result<Boolean>updateOrderSetting(@PathVariable("orderDate") String orderDate,@PathVariable("num") Integer num){
        return Result.build(orderSettingService.updateOrderSetting(orderDate,num));
    }
    /**
     * 清理预约数据
     */
    @GetMapping("/clearOssData")
    public Result<Boolean> clearOssData(){
        return Result.build(orderSettingService.clearOssData());
    }
}
